package org.algorithm.二师兄.数组.三月.第四周;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhx
 * @date 2022-03-21 星期一18:26
 *
 * url: https://leetcode-cn.com/problems/pascals-triangle/
 */
public class 杨辉三角I {

    public static void main(String[] args) {
        System.out.println(generate(5));
    }


    /**
     * 输入: numRows = 5
     * 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
     *
     * 输入: numRows = 1
     * 输出: [[1]]
     */
    public static List<List<Integer>> generate(int numRows) {
         // 定义一个List, List中存放每一行的数据.
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> row = new ArrayList<Integer>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    row.add(1);
                } else {
                    row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
                }
            }
            ret.add(row);
        }
        return ret;
    }
}
